<style>
   .order_no_wrap span{
        color:#007bb6;
        margin: 0 10px;
   }
</style>
<div class="nav-tabs-custom">
    <div class="tab-content">
        <ul class="nav nav-tabs">
            <li class="active">订单列表</li>
        </ul>
        <div class="tab-pane active" id="tab_2">
            <div class="box box-primary">
                <div class="box-header">
                    <form class="form-horizontal" method="get" action="{{ Request::url() }}" pjax-container>
                        <div class="form-group">
                            <div class="col-sm-2">
                                <input type="text" name="trade_no" value="{{ Request::input('trade_no') }}"
                                       class="form-control" placeholder="订单编号">
                            </div>
                            <div class="col-sm-2">
                                <input type="text" name="transaction_id" value="{{ Request::input('transaction_id') }}"
                                       class="form-control" placeholder="支付交易号">
                            </div>
                            <div class="col-sm-1">
                                <input type="text" name="transaction_id" value="{{ Request::input('transaction_id') }}"
                                       class="form-control" placeholder="物流单号">
                            </div>
                            <div class="col-sm-2">
                                <input type="text" name="phone" value="{{ Request::input('phone') }}"
                                       class="form-control" placeholder="收货人电话">
                            </div>
                            <div class="col-sm-2">
                                <input type="text" id="start_time" name="start_time" value="{{ Request::input('start_time') }}"
                                       class="form-control" placeholder="下单开始时间">
                            </div>
                            <div class="col-sm-2">
                                <input type="text" id="end_time" name="end_time" value="{{ Request::input('end_time') }}"
                                       class="form-control" placeholder="下单结束时间">
                            </div>
                            <span class="col-sm-1">
                                 <button type="submit" id="auth-button" class="btn btn-primary">查询</button>
                            </span>

                        </div>
                    </form>
                    <div class="btn-group" role="group" aria-label="">
                        <button type="button" status="all" @if(Request::input('status')=='all') class="btn btn-success" @else class="btn btn-default" @endif>全部</button>
                        <button type="button" status="0" @if(Request::input('status')=='0') class="btn btn-success" @else class="btn btn-default" @endif>未付款</button>
                        <button type="button" status="1" @if(Request::input('status')=='1') class="btn btn-success" @else class="btn btn-default" @endif>待备货</button>
                        <button type="button" status="4" @if(Request::input('status')=='4') class="btn btn-success" @else class="btn btn-default" @endif>待发货</button>
                        <button type="button" status="5" @if(Request::input('status')=='5') class="btn btn-success" @else class="btn btn-default" @endif>已发货</button>
                        <button type="button" status="6" @if(Request::input('status')=='6') class="btn btn-success" @else class="btn btn-default" @endif>已完成</button>
                    </div>
                    <div style="margin-top: 20px;display: flex;justify-content: flex-start;">
                        <button onclick="prepare_goods(null,2)" class="btn btn-primary btn-modify_address">批量备货</button>&nbsp;<button onclick="deliver_goods(null,2)" class="btn btn-primary btn-modify_address">批量发货</button>&nbsp;<button onclick="modify_address(2)" class="btn btn-primary btn-modify_address">修改收货地址</button>
                    </div>
                </div>
            </div>
            <!--代理统计表格开始-->
            <div class="box">
                <div class="box-body">
                    @foreach($orders as $key=> $order)
                        <div class="panel panel-default">
                        <div class="panel-heading" style="display: flex;justify-content: space-between;">
                            <div class="order_no_wrap" style="display: flex;justify-content: flex-start">
                                <input type="radio" @if($key==0) checked="checked" @endif name="order" value="{{$order->id}}" />
                                <span>订单编号: {{$order->trade_no}}</span>
                                <span>下单时间: {{$order->created_at}}</span>
                                <span>支付交易号: {{$order->transaction_id}}</span>
                            </div>
                            <span style="color: orange;">￥{{$order->amount}}</span>
                        </div>
                        <div class="panel-body">
                            <table class="table table-bordered table-hover text-center">
                                <thead>
                                <tr>
                                    <th>子订单编号</th>
                                    <th>商品名称</th>
                                    <th>商品类目</th>
                                    <th>商品图片</th>
                                    <th>商品售价</th>
                                    <th>实付金额</th>
                                    <th>收货人</th>
                                    <th>收货人电话</th>
                                    <th>订单状态</th>
                                    <th>发货时间</th>
                                    <th>操作</th>

                                </tr>
                                </thead>
                                <tbody>
                                @foreach ($order->order_products as $order_product)
                                    <tr>
                                        <td>{{$order_product->sub_trade_no}}</td>
                                        <td>{{$order_product->product->product_name}}</td>
                                        <td>{{$order_product->product->category_name}}</td>
                                        <td style="width:100px;"><img width="100%" src="{{$order_product->product->img_thum}}" /></td>
                                        <td>{{$order_product->price}}</td>
                                        <td>{{$order_product->amount}}</td>
                                        <td>{{$order->address->consignee}}</td>
                                        <td>{{$order->address->phone}}</td>
                                        {{--<td>{{$order_product->status}}</td>--}}
                                        @switch($order_product->status)
                                            @case(0)
                                                <td>待支付</td>
                                                @break
                                            @case(1)
                                                <td>待备货</td>
                                                @break
                                            @case(4)
                                                <td>待发货</td>
                                                @break
                                            @case(5)
                                                <td>已发货</td>
                                                @break
                                            @case(6)
                                                <td>已收货</td>

                                        @endswitch
                                        <td>{{$order_product->deliver_time}}</td>
                                        <td>
                                            <a href="/admin/order/order_detail?order_product_id={{$order_product->id}}">订单详情</a>&nbsp;
                                        @switch($order_product->status)
                                            @case(0)
                                            @break
                                            @case(1)
                                            <button onclick="prepare_goods({{$order_product->id}},1)" class="btn btn-success prepare_btn">备货</button>
                                            @break
                                            @case(4)
                                            <button onclick="deliver_goods({{$order_product->id}})" class="btn btn-info deliver_btn">发货</button>
                                            @break
                                            @case(5)
                                                @if($order->is_batch_ship !=1)
                                                    <button onclick="modify_address(1,{{$order_product->id}})" class="btn btn-info">修改收货地址</button>
                                                @endif
                                            @break
                                            @case(6)
                                            @endswitch
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                    @endforeach
                    <div style="display: flex;justify-content: flex-end;">
                        {{ $orders->appends(['status' => Request::input("status"),'trade_no'=>Request::input("trade_no")])->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="wrap-change-address" style="display: none;">
    <div style="padding-right: 20px;" id="change_address" >
        <div class="form-group row">
            <div class="col-sm-3">
                <label style="width: 100%;text-align: end" class="control-label">收货地址：</label>
            </div>
            <div class="col-sm-3">
                <select class="form-control prov" id="prov" name="prov" onchange="showCity(this)">
                    <option>请选择省份</option>
                    @foreach($provinces as $province)
                        <option value="{{$province->id}}">{{$province->name}}</option>
                    @endforeach
                </select>
            </div>

            <div class="col-sm-3">
                <select class="form-control city" id="city" onchange="showCountry(this)">
                    <option>请选择城市</option>
                </select>
            </div>

            <div class="col-sm-3">
                <select class="form-control country"  id="country" >
                    <option>请选择县区</option>
                </select>
            </div>
        </div>
        <div class="form-group row">
            <div class="col-sm-3">
                <label style="width: 100%;text-align: end" class="control-label">详细地址：</label>
            </div>
            <div class="col-sm-9">
                <input class="form-control detail_address" name="detail_address"  id="detail_address">
            </div>
        </div>
        <div class="form-group row">
            <div class="col-sm-3">
                <label style="width: 100%;text-align: end" class="control-label">联系人：</label>
            </div>
            <div class="col-sm-9">
                <input class="form-control contact" name="contact" id="contact">
            </div>
        </div>
        <div class="form-group row">
            <div class="col-sm-3">
                <label style="width: 100%;text-align: end" class="control-label">联系电话：</label>
            </div>
            <div class="col-sm-9">
                <input class="form-control phone" name="phone" id="phone">
            </div>
        </div>

    </div>
</div>
<div style="display: none;" id="deliver_modal_wrap">
    <div id="deliver_modal">
        <div class="form-group" style="display: flex;justify-content: flex-start;">
            <label style="line-height: 30px;text-align: end;" class="col-sm-3">物流公司：</label>
            <select type="text" name="ship_company_id" id="ship_company_id"
                    class="form-control col-sm-9 ship_company_id" >
                @foreach($ship_companys as $key=>$ship_company)
                    <option value="{{$ship_company->id}}">{{$ship_company->name}}</option>
                @endforeach
            </select>
        </div>
        <div class="form-group" style="display: flex;justify-content: flex-start;">
            <label style="line-height: 30px;text-align: end;" class="col-sm-3">物流单号：</label>
            <input type="text" name="ship_order_sn" id="ship_order_sn"
                   class="form-control col-sm-9 ship_order_sn">
        </div>
    </div>
</div>
<link rel="stylesheet" href="/bootstrap_switch/bootstrap_switch.css">

<script type="text/javascript" src="/bootstrap_switch/bootstrap_switch.js" ></script>
<script type="text/javascript">
    $('.btn-group button').on('click',function() {
        $('.btn-group button').attr('class','btn btn-default')
        $(this).attr('class','btn btn-success');
        location.href="?status=" + $(this).attr('status')
    })

    $(function () {
        $("#start_time").datetimepicker({
            format: 'YYYY-MM-DD HH:mm:ss'

        });

        $("#end_time").datetimepicker({
            format: 'YYYY-MM-DD HH:mm:ss'
            //format: 'yyyy-mm-dd hh:ii',//显示格式

            //language: 'zh-CN',
            //autoclose: 1,//选择后自动关闭
        });
    })

    function modify_address(type,id=null) {
        var dom = $('#wrap-change-address').html();
        if(type==2) {
              id = $('input[type^=radio]:checked').val()
        }
        console.log(id)
        var index =  layer.open({
            title:'修改收货地址',
            area:['600px','350px'],
            content: dom, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            yes:function (ind,layero) {
                layer.close(ind)
                var index2 = layer.load()
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    url: "{{admin_url('order/modify_address')}}",
                    data: {
                        order_product_id: (type==1)?id:'',
                        order_id: (type==2)?id:'',
                        province_id:$('.prov').eq(1).val(),
                        city_id:$('.city').eq(1).val(),
                        region_id:$('.country').eq(1).val(),
                        address:$('.detail_address').eq(1).val(),
                        contact:$('.contact').eq(1).val(),
                        phone:$('.phone').eq(1).val(),

                    },
                    success:function (res) {
                        layer.close(index2)
                        console.log(res)
                        if(res.code==0) {
                            layer.msg('修改成功')
                            location.reload()
                        }else{
                            layer.alert('修改失败:'.res.msg,{icon:5})
                        }

                    }
                })
            }
        });
    }

    function showCity(that) {
        var parent_id = $(that).val();
        var level = 2;

        $.ajax({
            type: 'post',
            dataType: 'json',
            url: "{{admin_url('order/get_area')}}",
            data: {
                level:level,
                parent_id:parent_id
            },
            success:function (re) {
                console.log(re)
                if(re.code==0) {
                    //res = re.data;
                    var cities = re.data;
                    var options = '';
                    for(var i=0,len=cities.length;i<len;i++) {
                        options += "<option value='"+ cities[i].id +"'"+">"+ cities[i].name +"</option>"
                    }
                    console.log(options)
                    console.log(cities)
                    $('.city').html(options)
                    showCountry($('.city')[1])
                }else{
                    res =false
                }
            }
        })

    }

    function showCountry(that) {
        var parent_id = $(that).val();
        var level = 3;

        $.ajax({
            type: 'post',
            dataType: 'json',
            url: "{{admin_url('order/get_area')}}",
            data: {
                level:level,
                parent_id:parent_id
            },
            success:function (re) {
                console.log(re)

                if(re.code==0) {
                    //res = re.data;
                    var districts = re.data;
                    var options = '';
                    for(var i=0,len=districts.length;i<len;i++) {
                        options += "<option value='"+ districts[i].id +"'"+">"+ districts[i].name +"</option>"
                    }
                    $('.country').html(options);
                }else{
                    res =false
                }
            }
        })

    }

    function prepare_goods(id,type) {
        var index = layer.load();
        if(type==2) {
            id = $('input[type^=radio]:checked').val()
        }
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: "{{admin_url('order/prepare_goods')}}",
            data: {
                order_product_id: type==1?id:'',
                order_id:type==2?id:''
            },
            success:function (res) {
                layer.close(index)
                if(res.code == 0) {
                    layer.msg('备货成功！')
                    location.reload();
                }else{
                    layer.alert('备货失败！'+res.msg,{icon:5})
                }
            }
        })
    }

    function deliver_goods(id,type=1) {
         var dom = $('#deliver_modal_wrap').html()
         var index =  layer.open({
            area:['600px','300px'],
            content: dom, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            yes:function (ind,layero) {
                layer.close(ind)
                var index2 = layer.load()
                if(type==2) {
                    id= $('input[type^=radio]:checked').val()
                }
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    url: "{{admin_url('order/deliver_goods')}}",
                    data: {
                        order_product_id: type==1?id:'',
                        order_id: type==2?id:'',
                        ship_company_id:$('.ship_company_id').eq(1).val(),
                        ship_order_sn:$('.ship_order_sn').eq(1).val()
                    },
                    success:function (res) {
                        layer.close(index2)
                        console.log(res)
                        if(res.code==0) {
                            layer.msg('发货成功')
                            location.reload()
                        }else{
                            layer.alert('发货失败！'+res.msg,{icon:5})
                        }

                    }
                })
            }
        });

    }

    function get_address_area(level,parent_id) {
        var res = null
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: "{{admin_url('order/get_area')}}",
            data: {
                level:level,
                parent_id:parent_id
            },
            success:function (re) {
                console.log(re)
                if(re.code==0) {
                    res = re.data;
                }else{
                    res =false
                }
            }
        })
        return res;
    }
</script>
